package com.east.service.sale.impl;


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.ContractPay;
import com.east.dao.ContractPayMapper;
import com.east.service.sale.ContractPayService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("contractPayService")
public class ContractPayServiceImpl implements ContractPayService{
	@Autowired
	ContractPayMapper contractPayMapper;

	@Override
	public Page<ContractPay> findByPage(Pagenation pagenation, String key,Integer cid) {
		Page<ContractPay> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("key", key);
		map.put("cid", cid);
		contractPayMapper.findAll(map);
		return startPage;
	}

	@Override
	public ContractPay findByOne(ContractPay contractPay) {
		return contractPayMapper.selectByPrimaryKey(contractPay.getCpId());
	}

	@Override
	public boolean saveContractPay(ContractPay contractPay) {
		ContractPay pay = new ContractPay();
		pay.setContractId(contractPay.getContractId());
		pay.setCpAmount(contractPay.getCpAmount());
		if(StringUtils.isNoneBlank(contractPay.getDateTime())) {
			pay.setCpPayTime(DateUtils.getDate(contractPay.getDateTime()));
		}
		pay.setCpResponsible(contractPay.getCpResponsible());
		pay.setCpStage(contractPay.getCpStage());
		pay.setCpType(contractPay.getCpType());
		pay.setCpUuid(UUID.randomUUID().toString());
		int insert = contractPayMapper.insert(pay);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateContractPay(ContractPay contractPay) {
		ContractPay pay = contractPayMapper.selectByPrimaryKey(contractPay.getCpId());
		pay.setCpAmount(contractPay.getCpAmount());
		if(StringUtils.isNoneBlank(contractPay.getDateTime())) {
			pay.setCpPayTime(DateUtils.getDate(contractPay.getDateTime()));
		}
		pay.setCpResponsible(contractPay.getCpResponsible());
		pay.setCpStage(contractPay.getCpStage());
		pay.setCpType(contractPay.getCpType());
		int insert = contractPayMapper.updateByPrimaryKey(pay);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean delContractPay(ContractPay contractPay) {
		int deleteByPrimaryKey = contractPayMapper.deleteByPrimaryKey(contractPay.getCpId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}

	
}
